# -*- coding: utf-8 -*-
# @Author: ystlong
# @Date:   2018-07-22 22:17:09
# @Last Modified by:   ystlong
# @Last Modified time: 2018-07-22 22:19:26

from twisted.internet import reactor
from twisted.internet.protocol import Protocol
from twisted.internet.endpoints import TCP4ClientEndpoint, connectProtocol

class Greeter(Protocol):
	def sendMessage(self, msg):
		self.transport.write("MESSAGE %s\n" % msg)

	def dataReceived(self, data):
		print data
        # self.peer.transport.write(data)

def gotProtocol(p):
	p.sendMessage("Hello")
	reactor.callLater(1, p.sendMessage, "This is sent in a second")
	reactor.callLater(2, p.transport.loseConnection)

point = TCP4ClientEndpoint(reactor, "localhost", 1234)
d = connectProtocol(point, Greeter())
d.addCallback(gotProtocol)
reactor.run()

# self.reactor.connectTCP(host, port, clientFactory)